package com.directv.drqp.parser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Parser {

	private static final char[] OPERATORS = { '=', '<', '>' };
	private static final String[] DELIMITERS = { "AND", "OR" };
	
	private static final Pattern PATTERN = Pattern.compile("(.*)([<>=~])(.*)");

	public static void main(String[] args) {
		String string = "Do this ~ than that";
		
		Matcher matcher = PATTERN.matcher(string);

		System.out.println(matcher.groupCount());
		if (matcher.find()) {
			for (int i = 1; i < matcher.groupCount() + 1; i++) {
				System.out.println(i + " mg: " + matcher.group(i));
			}
		}
	}

	private void parse(String query) {
		// we need a query cache so that we dont have to redo this each time.
		// but somehow we should be able to paramaterize the query by removing
		// the args and keeping the rest

		// first check for parenthases to group those

		// then check for && or || to group those

		// then break based on spaces after an operator and value. operator in
		// this case is = and after jjj a space or eol should cause this to get
		// grouped viewinghist = jjj
		List<Criteria> c = new ArrayList<Parser.Criteria>();

		for (char cc : OPERATORS) {
			int w = -1;
			while ((w = query.indexOf(cc)) != -1) {
				Criteria criteria = new Criteria();
				criteria.operator = query.charAt(w);
				criteria.operatorPos = w;
				c.add(criteria);
			}
		}

		Collections.sort(c);

		int lastEnd = 0;
		Iterator<Criteria> crIterator = c.iterator();
		while (crIterator.hasNext()) {
			crIterator.next();
			// crIterator.
		}

		for (int i = 0; i < c.size(); i++) {
			Criteria criteria = c.get(i);
			// if(c.)
		}
		for (Criteria criteria : c) {
			criteria.start = 0;
			// criteria.end = query.indexOf(ch)
		}
	}

	private class Criteria implements Comparable<Criteria> {
		int start;
		int end;
		int operatorPos;
		int operatorIndex;

		String what;
		char operator;
		String parameter;

		public int compareTo(Criteria o) {
			// TODO Auto-generated method stub.
			if (operatorPos == o.operatorPos) {
				return 0;
			}

			return operatorPos > o.operatorPos ? -1 : 1;
		}
	}
}
